package com.jifen.common;

import java.util.HashMap;

public class CommonResult {

	/**
	 * code:成功
	 */
	public static final int SUCCESS_CODE = 0;

	/**
	 * code:错误
	 */
	public static final int ERROR_CODE = 500;

	public static Result setResult(int code, Object data, String msg) {
		Result result = new Result();
		result.setCode(code);
		result.setData(data);
		result.setMsg(msg);
		return result;
	}

	/**
	 * 成功
	 *
	 * @return
	 */
	public static Result createSuccessResult() {
		Result result = new Result();
		result.setCode(SUCCESS_CODE);
		result.setMsg("成功");
		return result;
	}

	/**
	 * 成功
	 *
	 * @param data
	 * @return
	 */
	public static Result createSuccessResult(Object data) {
		Result result = new Result();
		result.setCode(SUCCESS_CODE);
		result.setData(data);
		result.setMsg("成功");
		return result;
	}

	/**
	 * 失败
	 * @return
	 */
	public static Result createErrorResult() {
		Result result = new Result();
		result.setCode(ERROR_CODE);
		result.setMsg("服务器开小差了...");
		return result;
	}

	/**
	 * 失败
	 * @param msg
	 * @return
	 */
	public static Result createErrorResult(String msg) {
		Result result = new Result();
		result.setCode(ERROR_CODE);
		result.setMsg(msg);
		return result;
	}

	/**
	 * 带失败错误码和错误信息
	 * 
	 * @param e
	 * @return
	 */
	public static Result createErrorResult(MyException e) {
		Result result = new Result();
		result.setCode(e.getCode());
		result.setMsg(e.getMsg());
		result.setData(new HashMap<>());
		return result;
	}
	
	/**
	 * 失败
	 * 
	 * @param data
	 * @param msg
	 * @return
	 */
	public static Result createErrorResult(Object data, String msg) {
		Result result = new Result();
		result.setCode(ERROR_CODE);
		result.setData(data);
		result.setMsg(msg);
		return result;
	}
}
